package fr.poy.mygluco.client.ui;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.IconButton;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuBar;
import com.extjs.gxt.ui.client.widget.menu.MenuBarItem;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

import fr.poy.mygluco.client.App;
import fr.poy.mygluco.client.gxt.AppLogsAsyncCallback;

public class Main extends Viewport  {
	Status status;
	public Main() {
		
		//NORTH
		this.setLayout(new BorderLayout()); 
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH , 40 );
		this.add(
				new HtmlContainer(
					"<div class='x-toolbar' style='color:#15428b;font-family: tahoma, arial; font-size: 22px;font-weight: bold;padding: 5px;'>"
					+ "<img src='images/logo-app.png' style='height:25px;' valign='middle'>"
					+ "&nbsp;MyGluco&nbsp;" + "</div>")	
				, northData);
 		  
		//SOUTH
		ToolBar tb = new ToolBar();  
	    status = new Status(); 
	    status.setWidth(400);
	    tb.add(status);
	    tb.add(new FillToolItem()); 
	    BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH,25);
	    this.add(tb,southData);
	    Registry.register("status", status) ;
	    
	    
	    ToolBar toolBar = new ToolBar();    
	    
	    Button btn = new Button(App.getMessages().settingsToolbarButton(), IconHelper.createStyle("toolbar-settings"));  
	    btn.addSelectionListener(new SelectionListener<ButtonEvent>() { 
	    	public void componentSelected(ButtonEvent ce) {
				ProfileSettings.getInstance().show();
			} 
		}); 
	    toolBar.add(btn);
	    
	    btn = new Button(App.getMessages().importToolbarButton(), IconHelper.createStyle("toolbar-import"));   
	    btn.addSelectionListener(new SelectionListener<ButtonEvent>() { 
	    	public void componentSelected(ButtonEvent ce) {
				UploadData.getInstance().show();
			} 
		}); 
	    toolBar.add(btn);
	    toolBar.add(new SeparatorToolItem());  
	    
	    btn = new Button("Admin");     
	    Menu menuAdmin = new Menu(); 
	    btn.setMenu(menuAdmin); 
	    toolBar.add(btn);
	    
	    MenuItem mi = new MenuItem("del records",IconHelper.createStyle("toolbar-delete"));
	    mi.addSelectionListener(new SelectionListener<MenuEvent>() { 
			public void componentSelected(MenuEvent ce) { 
				MessageBox.confirm("DEL", "confirmer la suppression !!! ",new Listener<MessageBoxEvent>() {
					public void handleEvent(MessageBoxEvent be) {
						if (MessageBox.CANCEL.equals(be.getButtonClicked().getText()))
							return;
						App.getService().removeAllRecords(new AppLogsAsyncCallback());
					} 
				});   
			} 
		}); 
	    menuAdmin.add(mi);  
	    
	    mi = new MenuItem("Export XML",IconHelper.createStyle("toolbar-save"));
	    mi.addSelectionListener(new SelectionListener<MenuEvent>() { 
			public void componentSelected(MenuEvent ce) { 
				Window.Location.assign(   
						GWT.getModuleBaseURL() +"download?type=exportXML"
						//+"&session="+sessionId 
						); 
			} 
		}); 
	    menuAdmin.add(mi);  
	     
	    
	    
	    toolBar.add(new FillToolItem());    
	    toolBar.add(new Text(App.getUserProfile().getUserEmail())); 
	    toolBar.add(new SeparatorToolItem());  
	    IconButton lo = new IconButton("toolbar-logoff" , new SelectionListener<IconButtonEvent>() { 
			public void componentSelected(IconButtonEvent ce) { 
				com.google.gwt.user.client.Window.Location.replace(
							App.getUserProfile().getLogoffUrl()
						); 
			} 
		}); 
	    lo.setToolTip(App.getMessages().logoff());
	    toolBar.add(lo); 
	    
	    //-----------------------
	    ContentPanel center = new ContentPanel(new FitLayout());  
	    center.setBodyBorder(false);
	    center.setHeaderVisible(false);
	    center.setTopComponent(toolBar);
	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
	    this.add(center,centerData);
	    
	    TabPanel tabPanel = new TabPanel();   
	    tabPanel.setResizeTabs(true);  
	    tabPanel.setAnimScroll(true);  
	    tabPanel.setTabScroll(true);  
	    tabPanel.setCloseContextMenu(true);
	    center.add(tabPanel);
	    
	    TabItem ti = new TabItem(App.getMessages().logBookTabTitle()); 
		ti.setIconStyle("logbook");
		ti.setLayout(new FitLayout());
	    LogBook b=new LogBook();
	    ti.add(b);
	    UploadData.getInstance().addAppListener(b);
	    tabPanel.add(ti); 
	}
 
}
